package com.dkts.oil.util;



import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文件解析工具类.
 *
 * @author heguanglong
 * @version 1.0.0
 * @date 2024-06-18
 * @description
 */
public class FileUtil {
    public static List<String[]> csvToList(String url, String charsetName) throws Exception {
        if ("".equals(url)) {
            throw new Exception("csv文件url地址不能为空！");
        }
        File csvFile = new File(url);
        if (!csvFile.exists()) {
            throw new Exception("csv文件不存在！");
        }

        if (!csvFile.isFile()) {
            throw new Exception("csv文件不能是目录");
        }
        List<String[]> dataList = new ArrayList<>(1024);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(csvFile), charsetName);
        try (BufferedReader br = new BufferedReader(isr)) {
            String line;
            while ((line = br.readLine()) != null) {
                dataList.add(line.split(","));
            }
            br.close();
            isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return dataList;
    }

    /**
     * 读取指定目录下面的文件
     *
     * @param url 文件目录地址
     * @return List<File> 文件
     */
    public static List<File> readDirectoryFiles(String url) throws Exception {
        if ("".equals(url)) {
            throw new Exception("读取的文件目录不能为空！");
        }
        File rootDir = new File(url);
        if (!rootDir.exists()) {
            throw   new Exception("目录不存在！");
        }
        List<File> directories = new ArrayList<>();
        readDirectories(rootDir, directories);
        if (directories.size() == 0) {
            throw new Exception("目录为空！");
        }
        return directories;
    }

    private static void readDirectories(File dir, List<File> directories) {
        if (dir.isDirectory()) {
            directories.add(dir);
            File[] subDirs = dir.listFiles(File::isDirectory);
            if (subDirs != null) {
                for (File subDir : subDirs) {
                    readDirectories(subDir, directories);
                }
            }
        }
    }

    /**
     * 读取文件，返回指定分割付 指定列的数据 返回map对象
     *
     * @param filePath
     * @param splitter
     * @param index
     * @return
     */
    public static List<Map<String, Object>> readFile(String filePath, String splitter, int index, String charsetName, String name) throws Exception {
        List<Map<String, Object>> returnDataMap = new ArrayList<>(1024);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), charsetName);
        try (BufferedReader br = new BufferedReader(isr)) {
            String line;
            while ((line = br.readLine()) != null) {
//                Map<String, Object> map = JSONArray.parseObject(line.split(splitter)[index], Map.class);
                Map<String, Object> map = JacksonUtils.strToObj(line.split(splitter)[index], Map.class);
                map.put("fileName", name);
                returnDataMap.add(map);
            }
            br.close();
            isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return returnDataMap;
    }

    public static void deleteFile(String url) {
        try {
            File file = new File(url);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
